{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Online Python Tutor\n",
"\n",
"This notebook shows how to create a cell magic for calling Online Python Tutor.\n",
"\n",
"First, we define the code to embed the tutor, and register the magic:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.display import HTML, display\n",
"from IPython.core.magic import register_line_cell_magic\n",
"import urllib\n",
"\n",
"@register_line_cell_magic\n",
"def tutor(line, cell):\n",
" code = urllib.urlencode({\"code\": cell})\n",
" display(HTML(\"\"\"\n",
" \n",
" \"\"\" % code))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we write some code:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0\n",
"0 1\n",
"0 2\n",
"1 0\n",
"1 1\n",
"1 2\n",
"2 0\n",
"2 1\n",
"2 2\n",
"45\n"
]
}
],
"source": [
"matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"\n",
"sum = 0\n",
"for i in range(len(matrix)):\n",
" for j in range(len(matrix[i])):\n",
" row = matrix[i]\n",
" sum += row[j]\n",
" print i, j\n",
"print sum"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see it execute line my line, we simply prefix the cell with our new `%%tutor` cell magic:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%tutor\n",
"matrix = [[1, 2, 3], \n",
" [4, 5, 6], \n",
" [7, 8, 9]]\n",
"\n",
"sum = 0\n",
"for i in range(len(matrix)):\n",
" for j in range(len(matrix[i])):\n",
" row = matrix[i]\n",
" sum += row[j]\n",
" print i, j\n",
"print sum"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}